﻿using _网易云.Model;
using _网易云.Utility;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _网易云
{
    [Obsolete("使用NetEaseProvider会更好的")]
    public class SongListSearch
    {
        private static Logger logger = new Logger(typeof(SongListSearch));
        public static List<Song> Crawler(string url)
        {
            List<Song> songLists = new List<Song>();
            try
            {
                string html = HttpHelper.DownloadSongList("http://music.163.com" + url);

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                string fristPath = "//ul[contains(concat(' ', normalize-space(@class), ' '), 'f-hide')]";
                HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes(fristPath);
                foreach (HtmlNode node in nodeList)
                {
                    songLists.AddRange(GetSongs(node.InnerHtml));
                }
            }
            catch (Exception ex)
            {
                logger.Error("CrawlerMuti出现异常", ex);
            }
            return songLists;
        }

        private static List<Song> GetSongs(string html)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            string path = "//a";
            HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes(path);
            List<Song> songList = new List<Song>();
            foreach (HtmlNode node in nodeList)
            {
                songList.Add(new Song
                {
                    Title = node.InnerText,
                    Url = node.Attributes["href"].Value,
                });
            }
            return songList;
        }
    }
}
